package org.ikastola

import ikastola.Constants;

import org.springframework.dao.DataIntegrityViolationException
import org.springframework.transaction.annotation.Transactional;
import grails.plugins.springsecurity.Secured

@Secured(['ROLE_ADMIN'])
class DiscountController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [discountInstanceList: Discount.findAllByDeleted(Constants.NOT_DELETED,params), discountInstanceTotal: Discount.countByDeleted(Constants.NOT_DELETED)]
    }

    def create() {
        [discountInstance: new Discount(params)]
    }
	
	@Transactional
    def save() {
		def feeInstance = Fee.get(params.fee)
		if (feeInstance) {
			params.fee = feeInstance
		}
		def discountInstance = new Discount(params)
		discountInstance.setValue(new Double(params.value))
		discountInstance.setDeleted(Constants.NOT_DELETED)
		discountInstance.setClassCreated(new Date())
        if (!discountInstance.save(flush: true)) {
            render(view: "create", model: [discountInstance: discountInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'discount.label', default: 'Discount'), discountInstance.id])
        redirect(action: "show", id: discountInstance.id)
    }

    def show(Long id) {
        def discountInstance = Discount.get(id)
        if (!discountInstance || Constants.DELETED.equals(discountInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discount.label', default: 'Discount'), id])
            redirect(action: "list")
            return
        }

        [discountInstance: discountInstance]
    }

    def edit(Long id) {
        def discountInstance = Discount.get(id)
        if (!discountInstance || Constants.DELETED.equals(discountInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discount.label', default: 'Discount'), id])
            redirect(action: "list")
            return
        }

        [discountInstance: discountInstance]
    }

	@Transactional
    def update(Long id, Long version) {
        def discountInstance = Discount.get(id)
        if (!discountInstance || Constants.DELETED.equals(discountInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discount.label', default: 'Discount'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (discountInstance.version > version) {
                discountInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'discount.label', default: 'Discount')] as Object[],
                          "Another user has updated this Discount while you were editing")
                render(view: "edit", model: [discountInstance: discountInstance])
                return
            }
        }
		
		def feeInstance = Fee.get(params.fee)
		if (feeInstance) {
			params.fee = feeInstance
		}
        discountInstance.properties = params
		discountInstance.setValue(new Double(params.value))
		discountInstance.setClassUpdated(new Date())
        if (!discountInstance.save(flush: true)) {
            render(view: "edit", model: [discountInstance: discountInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'discount.label', default: 'Discount'), discountInstance.id])
        redirect(action: "show", id: discountInstance.id)
    }

	@Transactional
    def delete(Long id) {
        def discountInstance = Discount.get(id)
        if (!discountInstance || Constants.DELETED.equals(discountInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'discount.label', default: 'Discount'), id])
            redirect(action: "list")
            return
        }

		def services = Service.findAllByDiscounts(discountInstance)
		if (services) {
			flash.message = "El Descuento/Beca no puede ser eliminado/a dado que est&aacute; asignado/a a un Alumno."
			redirect(action: "list")
			return
		}
		
        try {
			discountInstance.setDeleted(Constants.DELETED)
			discountInstance.setClassUpdated(new Date())
			discountInstance.save(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'discount.label', default: 'Discount'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'discount.label', default: 'Discount'), id])
            redirect(action: "show", id: id)
        }
    }
}
